<?php
include '../include/functions.php';
//set session save path
session_save_path($session_path);
// Initialize a session. 
session_start();
$count++;
$sessionId = session_id();
if((!$userID) || (!$start))
{
	header("Location: schedule.php"); /* Redirect browser */
	/* Make sure that code below does not get executed when we redirect. */
	exit;
}
//require_once('../include/QuickPicks.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//Dtd html 4.01 Transitional//EN"
"http://www.w3.org/tr/html4/loose.dtd">
<html>
  <head>
    <title>JoshHughes.com NHL Hockey Pool</title>
	<link rel="shortcut icon" href="http://hockey.joshhughes.com/NHL/images/nhlFavicon.gif" />
	<link rel="stylesheet" href="http://hockey.joshhughes.com/css/NHL.css" type="text/css">
	<script src="http://hockey.joshhughes.com/scripts/prototype.js" type="text/javascript"></script>
	<script src="http://hockey.joshhughes.com/scripts/scriptaculous.js" type="text/javascript"></script>
	<script src="http://hockey.joshhughes.com/scripts/hockey.js" type="text/javascript"></script>
  </head>
<body>
<div id="pagewidth" >
	<div id="header" >
	
		<?PHP include_once("layout/header.html"); ?>
		
	</div>
			<div id="wrapper" class="clearfix" > 
			<div id="maincol" >


			<?php
				  
				if((time() - $start)  > 1200)//if session is more than 20 mins old
				{	//logout: DO NOT SUBMIT PICKS!!!
				?>0
					<SCRIPT language="JavaScript">
						<!--0
						window.location="logoff.php?timeout=true";
						//-->
					</SCRIPT>
				<?
				}
				else
				{
					if ($userID)
					{	
						//Connect To Database
						dbConnect();
						
						//Get this week
						//$CurrWeek = getWeek("NHL");
						
						$CanChangePick = array_fill(1, 7, true);
						
						for ($Num = 1; $Num <= 7; $Num++)
						{
							$sql_string = "SELECT PickEmailed, Home, Away FROM NHLSchedule WHERE GameNum = $Num AND Week = $Week";
							$result = mysql_query($sql_string) or die("Cannot query the database.<br/>");
							$myrow = mysql_fetch_array($result);
							if ($myrow["Home"] !="")
							{
								$HomeTeams[$Num] = $myrow["Home"];
								$AwayTeams[$Num] = $myrow["Away"];
								if ($myrow["PickEmailed"] == 1)
									$CanChangePick[$Num] = false;
							}
						}
						//Set DoQuery flag to false, if no pick were passed there is no need to query
						$doQuery = false;									
						
						echo "<H1>Your Series $Week Picks</H1>";										
						
						if ($Submit == "Submit Picks")
						{
							//Write SQL statment to insert values
							$SQL = "INSERT INTO NHLPicks (UserID, Week";
							if ($Game1 != "") {
								if ($CanChangePick[1] == true) {
									$SQL = $SQL.", Game1";
									$doQuery = true;
								} else {
									echo "Game1 pick ($AwayTeams[1] @ $HomeTeams[1]) cannot be updated, this game is already over or in progress.<br/>";
								}
							} 
							if ($Game2 != "") {
								if ($CanChangePick[2] == true) {
									$SQL = $SQL.", Game2";
									$doQuery = true;
								} else {
									echo "Game2 pick ($AwayTeams[2] @ $HomeTeams[2]) cannot be updated, this game is already over or in progress.<br/>";
								}
							}
							if ($Game3 != "") {
								if ($CanChangePick[3] == true) {
									$SQL = $SQL.", Game3";
									$doQuery = true;
								} else {
									echo "Game3 pick ($AwayTeams[3] @ $HomeTeams[3]) cannot be updated, this game is already over or in progress.<br/>";
								}
							}
							if ($Game4 != "") {
								if ($CanChangePick[4] == true) {
									$SQL = $SQL.", Game4";
									$doQuery = true;
								} else {
									echo "Game4 pick ($AwayTeams[4] @ $HomeTeams[4]) cannot be updated, this game is already over or in progress.<br/>";
								}
							}
							if ($Game5 != "") {
								if ($CanChangePick[5] == true) {
									$SQL = $SQL.", Game5";
									$doQuery = true;
								} else {
									echo "Game5 pick ($AwayTeams[5] @ $HomeTeams[5]) cannot be updated, this game is already over or in progress.<br/>";
								}
							}
							if ($Game6 != "") {
								if ($CanChangePick[6] == true) {
									$SQL = $SQL.", Game6";
									$doQuery = true;
								} else {
									echo "Game6 pick ($AwayTeams[6] @ $HomeTeams[6]) cannot be updated, this game is already over or in progress.<br/>";
								}
							}
							if ($Game7 != "") {
								if ($CanChangePick[7] == true) {
									$SQL = $SQL.", Game7";
									$doQuery = true;
								} else {
									echo "Game7 pick ($AwayTeams[7] @ $HomeTeams[7]) cannot be updated, this game is already over or in progress.<br/>";
								}
							}
							$SQL = $SQL.") VALUES ($UserID, $Week";
							if (($Game1 != "") && ($CanChangePick[1] == true)) {
								$SQL = $SQL.", '$Game1'";
							}
							if (($Game2 != "") && ($CanChangePick[2] == true)) {
								$SQL = $SQL.", '$Game2'";
							}
							if (($Game3 != "") && ($CanChangePick[3] == true)) {
								$SQL = $SQL.", '$Game3'";
							}
							if (($Game4 != "") && ($CanChangePick[4] == true)) {
								$SQL = $SQL.", '$Game4'";
							}
							if (($Game5 != "") && ($CanChangePick[5] == true)) {
								$SQL = $SQL.", '$Game5'";
							}
							if (($Game6 != "") && ($CanChangePick[6] == true)) {
								$SQL = $SQL.", '$Game6'";
							}
							if (($Game7 != "") && ($CanChangePick[7] == true)) {
								$SQL = $SQL.", '$Game7'";
							}							
							$SQL = $SQL.")";
						}
						elseif ($Submit == "Update Picks")
						{
							//Write SQL statment to insert values
							$SQL = "UPDATE NHLPicks SET ";
							if ($Game1 != "") {
								if ($CanChangePick[1] == true) {
									$SQL = $SQL."Game1 = '$Game1', ";
									$doQuery = true;
								} else {
									echo "Game1 pick ($AwayTeams[1] @ $HomeTeams[1]) cannot be updated, this game is already over or in progress.<br/>";
								}
							} 
							if ($Game2 != "") {
								if ($CanChangePick[2] == true) {
									$SQL = $SQL."Game2 = '$Game2', ";
									$doQuery = true;
								} else {
									echo "Game2 pick ($AwayTeams[2] @ $HomeTeams[2]) cannot be updated, this game is already over or in progress.<br/>";
								}
							}
							if ($Game3 != "") {
								if ($CanChangePick[3] == true) {
									$SQL = $SQL."Game3 = '$Game3', ";
									$doQuery = true;
								} else {
									echo "Game3 pick ($AwayTeams[3] @ $HomeTeams[3]) cannot be updated, this game is already over or in progress.<br/>";
								}
							} 
							if ($Game4 != "") {
								if ($CanChangePick[4] == true) {
									$SQL = $SQL."Game4 = '$Game4', ";
									$doQuery = true;
								} else {
									echo "Game4 pick ($AwayTeams[4] @ $HomeTeams[4]) cannot be updated, this game is already over or in progress.<br/>";
								}
							} 
							if ($Game5 != "") {
								if ($CanChangePick[5] == true) {
									$SQL = $SQL."Game5 = '$Game5', ";
									$doQuery = true;
								} else {
									echo "Game5 pick ($AwayTeams[5] @ $HomeTeams[5]) cannot be updated, this game is already over or in progress.<br/>";
								}
							} 
							if ($Game6 != "") {
								if ($CanChangePick[6] == true) {
									$SQL = $SQL."Game6 = '$Game6', ";
									$doQuery = true;
								} else {
									echo "Game6 pick ($AwayTeams[6] @ $HomeTeams[6]) cannot be updated, this game is already over or in progress.<br/>";
								}
							} 
							if ($Game7 != "") {
								if ($CanChangePick[7] == true) {
									$SQL = $SQL."Game7 = '$Game7', ";
									$doQuery = true;
								} else {
									echo "Game7 pick ($AwayTeams[7] @ $HomeTeams[7]) cannot be updated, this game is already over or in progress.<br/>";
								}
							} 
							
							$SQL = substr($SQL, 0, strlen($SQL)-2);
							$SQL = $SQL." WHERE UserID =$UserID AND Week = $Week";
						}
						//Open Record Set using SQL Query
						if ($doQuery)
						{
							mysql_query($SQL) or die("Cannot query the database.<br/>");
							$filename = "notes/week".$Week.".txt";
							$fp = fopen($filename,"a");
							fputs($fp,date('m-d-Y H:i:s')." - ".$SQL."\n");
							fclose($fp);
						}
						
						if (($Game1 != "") && ($CanChangePick[1] == true)) {echo "Game 1 pick = $Game1<br/>";}
						if (($Game2 != "") && ($CanChangePick[2] == true)) {echo "Game 2 pick = $Game2<br/>";}
						if (($Game3 != "") && ($CanChangePick[3] == true)) {echo "Game 3 pick = $Game3<br/>";}
						if (($Game4 != "") && ($CanChangePick[4] == true)) {echo "Game 4 pick = $Game4<br/>";}
						if (($Game5 != "") && ($CanChangePick[5] == true)) {echo "Game 5 pick = $Game5<br/>";}
						if (($Game6 != "") && ($CanChangePick[6] == true)) {echo "Game 6 pick = $Game6<br/>";}
						if (($Game7 != "") && ($CanChangePick[7] == true)) {echo "Game 7 pick = $Game7<br/>";}
						
						echo "<br/><a href=\"picks.php?Weeknum=$Week\" class=\"permLink\">Go To &quot;Picks&quot; Page</a>";
						echo "<br/><a href=\"picks.php?Weeknum=".($Week + 1)."\" class=\"permLink\">Make Picks for Series ".($Week + 1)."</a>";
						//echo "<br/><a href=\"weighpicks.php?Weeknum=$Week\" class=\"permLink\">Go To &quot;Weigh Picks&quot; Page</a>";
						//echo "<br/><a href=\"survivor.php?Weeknum=$Week\" class=\"permLink\">Go To &quot;Survivor Picks&quot; Page</a>";
					}
					else		//If no $userID
					{
						echo "<H2>You must be registered member of this pool site, and you must be logged in to submit picks.</H1><p></p>";
						echo "<p>If you are a registered member, and you are logged in, then you must not of made the playoffs!";
					}//end if ($UserID)
					
			}//end if (time() - $start) < 1200
			
	?>
			
			</div>
			<div id="leftcol" >
			
				<?PHP include_once("layout/left_column.php"); ?>
		
			</div>
			
		</div>
		
		<div id="footer" >
		
			<?PHP include_once("layout/footer.html"); ?>
			
		</div>
</div>
</body>
</html>
